(function(_,$){
	_.Html.widget = "<div class='jdmer_widget'>"+
	"	<div class='widget_toolbar'>"+
	"		<div class='widget_buttons'><div class='wid_button wid_close'></div>"+
	"		<div class='wid_button wid_fixed wid_fixed2'></div>"+
	"		<div class='wid_button wid_setting'></div></div>"+
	"	</div>"+
	"	<div class='widget_content'></div>"+
	"</div>";
	
	var Widget = function(_config){
		this.config = {
			url : null,
			width : 200,
			height : 300,
			setting : false
		};
		_.extend(this,_.UI);
		$.extend(this.config,_config);
		
		var jq = {
			obj : null,
			toolbar : null,
			content : null,
			buttons : null,
			btn_close : null,
			btn_fixed : null,
			btn_setting : null
		};
		var htmlBase = _.Html.widget;	//html原型
		
		/**
		 * 显示当前对象
		 */
		this.display = function(){
			initjQuery();
			initDraggable();
			initEvent();
			this.id(this.config.id);
			this.container(this.config.container);
			this.top(this.config.top);
			this.left(this.config.left);
			this.width(this.config.width);
			this.height(this.config.height);
			this.url(this.config.url);
			jq.obj.data("jDreamerObject",this);
			jq.obj.css("position","absolute");
			if(this.config.setting){
				jq.btn_setting.show();
			}else{
				jq.btn_setting.hide();
			}
			return this;
		};
		
		this.destroy = function(){
			if(jq.obj)
				jq.obj.remove();
			clearjQuery();
			delete this;
			delete _this;
		};
		
		this.url = function(_url){
			if(_url == undefined){
				return this.config.url;
			}else{
				this.config.url = _url;
				if(jq.obj){
					jq.content.loader({
						url:this.config.url
					});
				}
				return this;
			}
		};
		
		/**
		 * 设置高度
		 */
		this.height = function(_height){
			if(_height == undefined){
				return this.config.height;
			}else{
				this.config.height = _height;
				if(jq.obj){
					jq.obj.height(this.config.height + jq.toolbar.height());
					jq.content.height(this.config.height);
				}
				return this;
			}
		};
		
		/**
		 * 设置宽度
		 */
		this.width = function(_width){
			if(_width == undefined){
				return this.config.width;
			}else{
				this.config.width = _width;
				if(jq.obj){
					jq.obj.width(this.config.width);
					jq.toolbar.width(this.config.width);
					jq.content.width(this.config.width);
				}
				return this;
			}
		};
		
		var initEvent = function(){
			if(jq.obj){
				jq.obj.mouseover(function(){
					jq.buttons.show();
				}).mouseout(function(){
					jq.buttons.hide();
				});
				jq.btn_close.click(function(){
					_this.destroy();
				});
				jq.btn_fixed.click(function(){
					if(jq.btn_fixed.hasClass("wid_fixed2")){
						jq.btn_fixed.removeClass("wid_fixed2");
						jq.btn_fixed.addClass("wid_fixed1");
						jq.obj.draggable( "option", "disabled", true );
					}else{
						jq.btn_fixed.removeClass("wid_fixed1");
						jq.btn_fixed.addClass("wid_fixed2");
						jq.obj.draggable( "option", "disabled", false );
					}
				});
			}
		};
		/**
		 * 初始化jQuery对象
		 * @returns
		 */
		var initjQuery = function(){
			jq.obj = $(htmlBase);
			_this.jQuery = jq.obj;
			jq.toolbar = jq.obj.children(".widget_toolbar");
			jq.buttons = jq.toolbar.children(".widget_buttons");
			jq.content = jq.obj.children(".widget_content");
			jq.btn_close = jq.buttons.children(".wid_close");
			jq.btn_fixed = jq.buttons.children(".wid_fixed");
			jq.btn_setting = jq.buttons.children(".wid_setting");
		};
		
		/**
		 * 清空jQuery对象
		 * @returns
		 */
		var clearjQuery = function(){
			jq.obj = null;
			_this.jQuery = null;
			jq.toolbar = null;
			jq.content = null;
			jq.btn_close = null;
			jq.btn_fixed = null;
			jq.btn_setting = null;
		};
		
		/**
		 * 初始化拖动事件
		 */
		var initDraggable = function(){
			jq.obj.draggable({
				addClasses: false,
				scroll : false,
				iframeFix : true,
				handle : jq.toolbar,
				containment:"parent",
				stop:function(){
					var dom = jq.obj.get(0);
					var top = dom.offsetTop;
					var left = dom.offsetLeft;
					_this.top(top);
					_this.left(left);
				}
			});
		};
		
		var _this = this;
	};
	_.Widget = Widget;
	_.widget = function(_config){
		return new _.Widget(_config);
	};
	
	if(!_.Changer){
		return ;
	}
	_.Changer.addHandler({
		name : "widget",
		selector : "div[changer='widget']",
		handler : function(objs , region){
			objs.each(function(i){
				try{
					var current = objs.eq(i);
					var config = current.attr("config");
					if(config){ config = eval("("+config+")"); }else{ config = {}; }	//转换config
					var widget = _.widget(config);
					widget.display();
					widget.jQuery.insertAfter(current);
					current.remove();
				}catch(e){
					alert(e);
				}
			});	
		}
	});
})(jDreamer,jQuery);